###################################################################
#### Figures and Additional Analysis for "Unpaved Road Ahead"  ####
###################################################################

library(ggplot2)
library(scales)
library(foreign)
library(plyr)
library(plyr)
library(haven)
library(Matching)

rm(list=ls(all=TRUE))
setwd("~/Dropbox/Jan_Audrey papers/Jan's do files for analysis/Research_Note_PBC/Budget_JoP_Submission/RR/Final/Replication/")

data <- read.dta("replication_data.dta")

#######################################
### PLOTS
#######################################

################
## Figure 1
################

test <- data[,c("year","ltotalexp_all_pc","lcapital_exp_pc","lgoods_exp_pc","lpersonnel_exp_pc")]
test <- na.omit(test)


pdf(file="PBC_ltotalpc_year.pdf",width=10)
ggplot(test,aes(factor(year),ltotalexp_all_pc))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="log(Total Expenditures pc)")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

pdf(file="PBC_lcapitalpc_year.pdf",width=10)
ggplot(test,aes(factor(year),lcapital_exp_pc))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="log(Capital Expenditures pc)")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

pdf(file="PBC_lgoodspc_year.pdf",width=10)
ggplot(test,aes(factor(year),lgoods_exp_pc))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="log(Goods and Services Expenditures pc)")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

pdf(file="PBC_lpersonnelpc_year.pdf",width=10)
ggplot(test,aes(factor(year),lpersonnel_exp_pc))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="log(Personnel Expenditures pc)")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

#################
## Figure 2
#################


zzTransparency <- 0.3
IV <- c("Capital Share","Goods Share","Personnel")
Estimate <- c(-0.0142,-0.0051,0.0193)
sd <- c(0.0053,0.0058,0.007)
min <- Estimate -1.96*sd
max <- Estimate + 1.96*sd
OutputPlot <- qplot(x=IV,y=Estimate,ymin=min,ymax=max,ylab = "Election Year Effect", xlab = NULL, geom = "blank" )
OutputPlot <- OutputPlot + geom_hline(yintercept = 0, lwd = I(9/12), colour = I(hsv(0/12, 7/12, 7/12)), alpha = I(10/12))
OutputPlot <- OutputPlot + geom_linerange(aes(size = 1), alpha = I(zzTransparency), colour = I(gray(0)))
OutputPlot <- OutputPlot + scale_size_continuous(guide="none")
OutputPlot <- OutputPlot +coord_flip()+ geom_point(aes(x = IV, y = Estimate,size=1), colour = I(gray(0))) + theme_bw()+theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
OutputPlot
pdf(file="PBC_election_year.pdf",width=10)
OutputPlot
dev.off()

# Effect of post Election Year
zzTransparency <- 0.3
IV <- c("Capital Share","Goods Share","Personnel")
Estimate <- c(0.0065,0.0099,-0.0165)
sd <- c(0.0036,0.0044,0.0062)
min <- Estimate -1.96*sd
max <- Estimate + 1.96*sd
OutputPlot <- qplot(x=IV,y=Estimate,ymin=min,ymax=max,ylab = "Post-Election Year Effect", xlab = NULL, geom = "blank" )
OutputPlot <- OutputPlot + geom_hline(yintercept = 0, lwd = I(9/12), colour = I(hsv(0/12, 7/12, 7/12)), alpha = I(10/12))
OutputPlot <- OutputPlot + geom_linerange(aes(size = 1), alpha = I(zzTransparency), colour = I(gray(0)))
OutputPlot <- OutputPlot + scale_size_continuous(guide="none")
OutputPlot <- OutputPlot +coord_flip()+ geom_point(aes(x = IV, y = Estimate,size=1), colour = I(gray(0))) + theme_bw()+theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
OutputPlot
pdf(file="PBC_post_election_year.pdf",width=10)
OutputPlot
dev.off()

# Effect of pre Election Year
zzTransparency <- 0.3
IV <- c("Capital Share","Goods Share","Personnel")
Estimate <- c(-0.00013,-0.0025,0.0027)
sd <- c(0.0024,0.0045,0.0055)
min <- Estimate -1.96*sd
max <- Estimate + 1.96*sd
OutputPlot <- qplot(x=IV,y=Estimate,ymin=min,ymax=max,ylab = "Pre-Election Year Effect", xlab = NULL, geom = "blank" )
OutputPlot <- OutputPlot + geom_hline(yintercept = 0, lwd = I(9/12), colour = I(hsv(0/12, 7/12, 7/12)), alpha = I(10/12))
OutputPlot <- OutputPlot + geom_linerange(aes(size = 1), alpha = I(zzTransparency), colour = I(gray(0)))
OutputPlot <- OutputPlot + scale_size_continuous(guide="none")
OutputPlot <- OutputPlot +coord_flip()+ geom_point(aes(x = IV, y = Estimate,size=1), colour = I(gray(0))) + theme_bw()+theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
OutputPlot
pdf(file="PBC_pre_election_year.pdf",width=10)
OutputPlot
dev.off()


##########################
## Appendix
##########################

## Appendix Figure 1

testm <- data.frame(testm)
colnames(testm) <- c("Year","Capital Share","Goods Share","Personnel Share")

test_melt <- melt(testm,id="Year")
colnames(test_melt) <- c("Year","Type","Share")

pdf(file="PBC_composition_year.pdf",width=10)
ggplot(test_melt, aes(x = factor(Year), y = Share, fill = Type)) +
  geom_bar(stat = "identity", width = .7) +
  scale_y_continuous(labels = percent_format())+ theme_bw()+ scale_fill_grey()+
  labs(x = "Year")
dev.off()


## Appendix Figure 2
test <- data[,c("year","capital_sh","goods_sh","personnel_sh")]
test <- na.omit(test)

pdf(file="PBC_capital_year.pdf",width=10)
ggplot(test,aes(factor(year),capital_sh))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="Share of Capital Expenditures")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

pdf(file="PBC_goods_year.pdf",width=10)
ggplot(test,aes(factor(year),goods_sh))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="Share of Goods and Services Expenditures")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

pdf(file="PBC_personnel_year.pdf",width=10)
ggplot(test,aes(factor(year),personnel_sh))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="Share of Personnel Expenditures")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()


## Appendix Figure 3
test <- data[,c("year","totalexp_all_pc","capital_exp_pc","goods_exp_pc","personnel_exp_pc")]
test <- na.omit(test)


pdf(file="PBC_totalpc_year.pdf",width=10)
ggplot(test,aes(factor(year),totalexp_all_pc/1000000))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="Total Expenditures pc")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

pdf(file="PBC_capitalpc_year.pdf",width=10)
ggplot(test,aes(factor(year),capital_exp_pc/1000000))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="Capital Expenditures pc")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

pdf(file="PBC_goodspc_year.pdf",width=10)
ggplot(test,aes(factor(year),goods_exp_pc/1000000))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="Goods and Services Expenditures pc")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()

pdf(file="PBC_personnelpc_year.pdf",width=10)
ggplot(test,aes(factor(year),personnel_exp_pc/1000000))+geom_boxplot()+
  theme_bw()+labs(x = "Year",y="Personnel Expenditures pc")+
  theme(axis.text=element_text(size=17),axis.title=element_text(size=17,face="bold"))
dev.off()


## Appendix Table 3
data_05 <- subset(data,data$year==2005)
bal1 <- MatchBalance(election_year~election_year_lead1+
                       +election_year_l+elected_leader_l+enp_all+golkar_share_all+
                       pdip_share_all+ServicesA+rev_natural_pc_l+gini_l+rev_total_pc_l+lpop_l
                     +poverty_pc_l+lgdppc_l,data=data_05)

